package cn.com.cfae.iras.doc.analyze.handUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class FormatTimeUtil {
    private static final Logger logger = LoggerFactory.getLogger(FormatTimeUtil.class);

    public String formatTime(String str){
        if (StringUtils.isEmpty(str)){
            return "";
        }
        StringBuffer timeBuff = new StringBuffer();
        String regex = "\\d+";
        List<String> times = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()){
            times.add(matcher.group());
        }
        if (CollectionUtils.isNotEmpty(times)){
            if (times.size()<=3){
                for (int i=0;i<times.size();i++){
                    String time = times.get(i);
                    if (time.length()==4)
                        continue;
                    if (time.length()<2){
                        times.set(i,"0"+time);
                    }
                }
                for (String time : times){
                    timeBuff.append(time);
                }

            }else if (times.size()>3){
                for (int i=0;i<times.size();i++){
                    String time = times.get(i);
                    if (time.length()==4)
                        continue;
                    if (time.length()<2){
                        times.set(i,"0"+time);
                    }
                }
                for (int i=0;i<3;i++){
                    timeBuff.append(times.get(i));
                }
            }
        }
        return  timeBuff.toString();
    }


    public long getTimeDValue(String sDate,String eDate, String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        long time1 = 0;
        long time2 = 0;

        try{
            cal.setTime(sdf.parse(sDate));
            time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(eDate));
            time2 = cal.getTimeInMillis();
        }catch(Exception e){
           logger.error(e.getMessage(),e);
        }
       return (time2-time1)/(1000*3600*24);
    }

    public String getTimeOfSet(String sDate,String type,int set,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Calendar rightNow = Calendar.getInstance();
        Date dt1 = null;
        try {
            Date dt=sdf.parse(sDate);
            rightNow.setTime(dt);
            if (StringUtils.isNotEmpty(type)&&"YEAR".equals(type)){
                rightNow.add(Calendar.YEAR,set);
            }else if (StringUtils.isNotEmpty(type)&&"MONTH".equals(type)){
                rightNow.add(Calendar.MONTH,set);
            }else if (StringUtils.isNotEmpty(type)&&"DAY".equals(type)){
                rightNow.add(Calendar.DAY_OF_YEAR,set);
            }
            dt1=rightNow.getTime();
        }catch (Exception e){
            logger.error(e.getMessage());
        }

       return sdf.format(dt1);

    }
}
